<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>子向父传值</title>
</head>

<body>
    <div class="root">
        <phone></phone>
    </div>
    <template id="km">
        <div>
            <h5>{{msg}}</h5>
            <button @click="sub">传递</button>
        </div>
    </template>
    <template id="kl">
        <div>
           
            <xing @send="accMsg"></xing>
            <h1 v-for="(item, index) in arr" :key="index">{{item.title}}-- {{msg}} </h1>
       
    </div>
    </template>
    <script src="./vue.js"></script>
    <script>
        //阻止vue在启动时生成生产提示
        Vue.config.productionTip = false;
        // 组件
        const xing = {
            template: '#km',
            data() {
                return {
                    msg: '256G'
                }
            },
            methods: {
                sub() {
                    this.$emit('send', this.msg)
                }
            },
        }
        const phone = {
            //报错？
            // template: `<h1 v-for='(item, index) in arr' :key='index'>{{item.title}}</h1>`,
            template: '#kl',
            components: {
                xing,
            },
            data() {
                return {
                    arr: [
                        { id: 1, title: '苹果', },
                        { id: 2, title: '小米', },
                        { id: 3, title: 'vivo', },
                    ],
                    msg: '',
                }
            },
            methods: {
                accMsg(msg) {
                    this.msg = msg
                }
            }
        }
        //创建vue实例
        new Vue({
            el: '.root',
            data: function () {
                return {
                    // name: '',
                }
            },
            components: {
                phone,
            }
        })
    </script>
</body>

</html>